﻿using BCCommon;
using BCCommon.Enums;
using BCDto.Website.User;
using BCService.TMS.Agent.AgentAreaRecord;
using BCService.Website.User;
using BCTMSRESTful.Configs;
using BCTMSRESTful.Controllers;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc.Filters;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;

namespace BCTMSRESTful.Controllers.ClientApp
{
    [RequireAuthorize(RoleType.User)]
    public class ClientAppBaseController : BCTMSRESTfulAPIController
    {
        public UserDto UserInfo { get; private set; }
        private IAgentAreaRecordService agentAreaRecordService;
        public override void OnActionExecuting(ActionExecutingContext context)
        {
            HttpContext httpContext = context.HttpContext;
            agentAreaRecordService = httpContext.RequestServices.GetService(typeof(IAgentAreaRecordService)) as IAgentAreaRecordService;
            if (httpContext.User.Identity.IsAuthenticated && httpContext.User.Identity.Name.StartsWith("User:"))
            {
                long userId;

                if (long.TryParse(httpContext.User.Identity.Name.Substring(5), out userId))
                {
                    #region 初始化服务
                  
                    IRedisService redisService = httpContext.RequestServices.GetService(typeof(IRedisService)) as IRedisService;
                    IUserService userService = httpContext.RequestServices.GetService(typeof(IUserService)) as IUserService;
                   
                    //IUserExtensionInfoService userExtensionInfoService = httpContext.RequestServices.GetService(typeof(IUserExtensionInfoService)) as IUserExtensionInfoService;
                    #endregion

                    this.UserInfo = userService.GetUserByUserId(userId);
                    //    if (this.UserInfo != null)
                    //    {
                    //        UserExtensionInfoDto userExtensionInfoDto = userExtensionInfoService.ClientAppAuthUserStatusValidation(userId);
                    //        if (userExtensionInfoDto != null)
                    //        {
                    //            this.UserInfo.UserExtensionInfo = userExtensionInfoDto;
                    //            if (userExtensionInfoDto.ValidationStatus != ValidationStatus.Success)
                    //            {
                    //                this.UserInfo.UserName = "未实名认证用户";
                    //            }
                    //        }
                    //        else
                    //        {
                    //            this.UserInfo.UserName = "未实名认证用户";
                    //        }
                    //    }
                }

                if (this.UserInfo == null)
                {
                    context.Result = this.Forbid();
                }
            }
            base.OnActionExecuting(context);
        }
        /// <summary>
        /// 获取实际运营代理商的代理商Id
        /// </summary>
        /// <param name="countyCode">区县编码</param>
        /// <returns></returns>
        protected long? GetAgentRecordIdByCountyCode(string countyCode)
        {
            if (this.agentAreaRecordService != null)
            {
                var agentRecordDto = this.agentAreaRecordService.GetLastLevelByCode(countyCode);
                if (agentRecordDto == null)
                {
                    return null;
                }
                else
                {
                    return agentRecordDto.AgentRecordId;
                }
            }
            return null;
        }
    }
}
